package net.osc.model.query;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import net.osc.model.BaseEntity;



/**
 * 动态查询拼接段
 * 
 * @author <a href=mailto:peng.wu@foxmail.com>wu.peng</a> 2012-9-7
 * 
 */
@Entity
@Table(name = "query_segment")
public class QuerySegment extends BaseEntity {

	/** 条件表达式 */
	private String condition;

	/** 内容 */
	private String text;

	/** 所属查询单元 */
	private QueryUnit queryUnit;

	/** 序号 */
	private Integer segmentOrder;

	@Column(name = "segment_order")
	public Integer getSegmentOrder() {
		return segmentOrder;
	}

	public void setSegmentOrder(Integer segmentOrder) {
		this.segmentOrder = segmentOrder;
	}

	@Lob
	@Column(name = "query_condition")
	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	@Lob
	@Column(name = "query_text")
	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	@ManyToOne
	@JoinColumn(name = "query_unit_id")
	public QueryUnit getQueryUnit() {
		return queryUnit;
	}

	public void setQueryUnit(QueryUnit queryUnit) {
		this.queryUnit = queryUnit;
	}

	@Override
	public int compareTo(Object o) {
		if (o instanceof QuerySegment) {
			QuerySegment qs = (QuerySegment) o;
			if (this.segmentOrder != null && qs.getSegmentOrder() != null) {
				int ret = this.segmentOrder.compareTo(qs.getSegmentOrder());
				if(ret != 0) {
					return ret;
				}
			}
		}
		return super.compareTo(o);
	}

}
